<?php

namespace app\api\controller;

use app\api\server\Promotion as ServerPromotion;
use app\api\server\User as ServerUser;
use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Frozenlog;
use app\common\model\MoneyLog;
use app\common\model\order\Order;
use app\common\model\performance\Log;
use app\common\model\ScoreLog;
use app\common\model\User;
use app\common\model\UserGroup;

/**
 * 推广接口
 */
class Promotion extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (收益统计)
     * @ApiMethod (GET)
     * @ApiMethod (POST)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function index()
    {
        $uid = $this->auth->id;
        $group_id = $this->auth->group_id;

        //创业专区（套餐）
        $entrepreneurial_zone = ['直推收益(套餐)', '平级收益(套餐)', '团队奖收益(套餐)', '伯乐奖收益(套餐)', '互助收益(套餐)'];
        //今日预估收益
        $zone['todaymoney'] = MoneyLog::where('user_id', $uid)->whereTime('createtime', 'today')->whereIn('memo', $entrepreneurial_zone)->sum('money');
        //本月预估收益
        $zone['monthmoney'] = MoneyLog::where('user_id', $uid)->whereTime('createtime', 'month')->whereIn('memo', $entrepreneurial_zone)->sum('money');
        //累计获取收益
        $zone['totalmoney'] = MoneyLog::where('user_id', $uid)->where('money', '>', 0)->whereIn('memo', $entrepreneurial_zone)->sum('money');


        //创业专区（零售）
        $entrepreneurial_zone_retail = ['直推收益(零售)', '平级收益(零售)', '代理级差收益(零售)'];
        //今日预估收益
        $retail['todaymoney'] = Frozenlog::where('user_id', $uid)->whereTime('createtime', 'today')->whereIn('memo', $entrepreneurial_zone_retail)->sum('money');
        //本月预估收益
        $retail['monthmoney'] = Frozenlog::where('user_id', $uid)->whereTime('createtime', 'month')->whereIn('memo', $entrepreneurial_zone_retail)->sum('money');
        //累计获取收益
        $retail['totalmoney'] = MoneyLog::where('user_id', $uid)->where('money', '>', 0)->whereIn('memo', $entrepreneurial_zone_retail)->sum('money');

        //非创业专区
        //今日预估收益
        $non_zone['todaymoney'] = Frozenlog::where('user_id', $uid)->whereTime('createtime', 'today')->where('memo', '直推收益(非创业专区')->sum('money');
        //本月预估收益
        $non_zone['monthmoney'] = Frozenlog::where('user_id', $uid)->whereTime('createtime', 'month')->where('memo', '直推收益(非创业专区')->sum('money');
        //累计获取收益
        $non_zone['totalmoney'] = MoneyLog::where('user_id', $uid)->where('money', '>', 0)->where('memo', '直推收益(非创业专区')->sum('money');


        //粉丝数量
        $count = User::where("pid", $uid)->count();
        // $count = count($nums);
        // if ($nums) {
        //     $count += User::whereIn("pid", $nums)->count();
        // }
        $this->success('请求成功', compact('zone', 'retail', 'non_zone', 'count'));
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (我的粉丝)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=一级粉丝，2=二级粉丝")
     * @ApiParams (name="group_id", type="string", required=true, sample="", description="粉丝身份")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function my_fans()
    {
        $param = request()->param();
        extract($param);
        $fans = new User;
        $uid = $this->auth->id;
        if (isset($type) && $type) {
            if ($type == 1) {
                $fans->where('pid', $uid);
            } else {
                $fans->where('tid', $uid);
            }
        } else {
            $fans->whereOr('pid', $uid);
            $fans->whereOr('tid', $uid);
        }
        if (isset($group_id) && $group_id) {
            $fans->where('group_id', $group_id);
        }
        $lists = $fans->with(['group'])->paginate();
        foreach ($lists as $k => $v) {
            //累计收益
            $v->earnings = MoneyLog::where('user_id', $v->id)->where('money', '>', 0)->sum('money');
            //业绩
            $v->performance = Log::where('user_id', $v->id)->where('performance', '>', 0)->sum('performance');
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (佣金明细)
     * @ApiMethod (GET)
     * @ApiParams (name="starttime", type="string", required=true, sample="", description="")
     * @ApiParams (name="endtime", type="string", required=true, sample="", description="")
     * @ApiParams (name="type", type="string", required=true, sample="", description="all=全部,1=创业专区套餐明细,2=创业专区零售业绩,3=非创业专区业绩")
     * @ApiParams (name="class", type="string", required=true, sample="", description="all=全部,1=收入,2=支出")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function commission_detail()
    {
        $param = request()->param();
        extract($param);
        $log = new MoneyLog;
        $uid = $this->auth->id;
        if (isset($starttime) && $starttime && isset($endtime) && $endtime) {
            $log->whereTime('createtime', 'between', [strtotime($starttime), strtotime($endtime . " 23:59:59")]);
        }
        if ($type != 'all') {
            if ($type == 1) {
                $log->whereIn('memo', ['直推收益(套餐)', '平级收益(套餐)', '团队奖收益(套餐)', '伯乐奖收益(套餐)', '互助收益(套餐)']);
            } else if ($type == 2) {
                $log->whereIn('memo', ['直推收益(零售)', '平级收益(零售)', '代理级差收益(零售)']);
            } else if ($type == 3) {
                $log->where('memo', '直推收益(非创业专区)');
            }
        }
        if ($class != 'all') {
            if ($class == 1) {
                $log->where('money_log.money', '>', 0);
            } else if ($class == 2) {
                $log->where('money_log.money', '<=', 0);
            }
        }
        $lists = $log->with(['user','userinfo'])->where('money_log.user_id', $uid)->order('money_log.id desc')->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (积分明细)
     * @ApiMethod (GET)
     * @ApiParams (name="starttime", type="string", required=true, sample="", description="")
     * @ApiParams (name="endtime", type="string", required=true, sample="", description="")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function point_detail()
    {
        $param = request()->param();
        extract($param);
        $log = new ScoreLog();
        $uid = $this->auth->id;
        if (isset($starttime) && $starttime && isset($endtime) && $endtime) {
            $log->whereTime('createtime', 'between', [strtotime($starttime), strtotime($endtime . " 23:59:59")]);
        }
        $lists = $log->where('user_id', $uid)->order('id desc')->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (分销订单)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=待返佣,2=已结算")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function distribution_order()
    {
        $param = request()->param();
        extract($param);
        $log = new Frozenlog;
        if ($type == 1) {
            $log->where('frozenlog.is_settle', 0);
        } elseif ($type == 2) {
            $log->where('frozenlog.is_settle', 1);
        }
        $lists =  $log->with(['user'])->where('order_id', '>', 0)->order('id desc')->paginate();
        foreach ($lists as $k => $v) {
            $v->order = Order::get($v->order_id, ['goods']);
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-01-05 
     * @ApiTitle (团队销售业绩)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="type", type="string", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function sales_performance()
    {
        $lists = (new ServerPromotion)->team_yj();
        $this->success('', $lists);
    }
}
